package cn.doitedu.dwh

import java.io.{ByteArrayInputStream, ByteArrayOutputStream, DataInputStream, DataOutputStream}

import org.roaringbitmap.RoaringBitmap

object BmpUtils {

  // 将一个bitmap对象转成序列化的字节数组
  def bmp2ByteArray(bmp:RoaringBitmap):Array[Byte] = {
    val out = new ByteArrayOutputStream()
    val dataOutput = new DataOutputStream(out)
    bmp.serialize(dataOutput)
    out.toByteArray
  }

  // 从一个字节数组中，反序列化出bitmap对象
  def byteArray2Bmp(barr:Array[Byte]):RoaringBitmap = {

    val bmp = RoaringBitmap.bitmapOf()

    val bain = new ByteArrayInputStream(barr)
    val dataIn = new DataInputStream(bain)

    bmp.deserialize(dataIn)

    bmp
  }


}
